﻿#region Using Directives

using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.IO;
using EnvDTE;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.TeamArchitect.PowerTools.Features;

#endregion

namespace FeatureBuilder.ContribLib.ValueProviders
{
    /// <summary>
    /// ValueProvider that returns the Active Document selected in the selected Project
    /// </summary>
    [DisplayName("SelectedItemNameProvider")]
    [Description("Returns the Active Document selected in the selected Project")]
    [Category("IDE")]
    public class SelectedItemNameProvider : ValueProvider
    {
        /// <summary>
        /// Gets the ServiceProvider that we will use to get a reference to DTE.
        /// </summary>
        [Import(typeof(SVsServiceProvider))]
        public IServiceProvider ServiceProvider { get; private set; }
        
        public DTE ProjectDTE { get; set; }

        #region Overrides

        public override object Evaluate()
        {
            object selectedItem = null;
            DTE projectDTE = this.ServiceProvider.GetService(typeof(DTE)) as DTE;

            try
            {
                selectedItem = Path.GetFileNameWithoutExtension(ProjectDTE.SelectedItems.Item(1).ProjectItem.get_FileNames(1));
            }
            catch (Exception)
            {}

            return selectedItem;
        }

        #endregion
    }
}

